/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package serveur;

import controlleur.ControllerGrille;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import jreversi.Coordonnee;
import moteur.Plateau;

/**
 *
 * @author Jean
 */
public class ReseauLan
{

    ServerSocket serv;
    Socket client;
    private static ReseauLan instance = null;
    InputStream ois;
    OutputStream oos;

    private ReseauLan()
    {
    }

    public static ReseauLan getInstance()
    {
        if (instance == null)
        {
            instance = new ReseauLan();
        }
        return instance;
    }
    
    public void clientCreation(InetAddress ip){
        try
        {
            client = new Socket(ip, 300);
            System.out.println("connecté");
            
            ois = client.getInputStream();
            oos = client.getOutputStream();
            this.listen();
        }
        catch (IOException ex)
        {
            Logger.getLogger(ReseauLan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void creation(InetAddress ip)
    {
        System.out.println("lol ?");
        try
        {
            serv = new ServerSocket(300, 1500, ip);
            System.out.println("lol?");
            client = serv.accept();
            System.out.println("Client ?");
            ois = client.getInputStream();
            oos = client.getOutputStream();
            //Ca veut dire que c'est a moi les blancs donc que je dois ecouter d'abord
            if(Plateau.getInstance().aQui('b') == 'H'){
                this.listen();
            }
        }
        catch (IOException ex)
        {
            Logger.getLogger(ReseauLan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void closeClient()
    {
        try
        {
            client.close();
        }
        catch (IOException ex)
        {
            Logger.getLogger(ReseauLan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void closeAll()
    {
        try
        {
            client.close();
            serv.close();
        }
        catch (IOException ex)
        {
            Logger.getLogger(ReseauLan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void send()
    {
        try
        {
            String str = Plateau.getInstance().getLastXClick() + "#" + Plateau.getInstance().getLastYClick();
            oos.write(str.getBytes());
            System.out.println(str);
        }
        catch (IOException ex)
        {
            Logger.getLogger(ReseauLan.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void listen()
    {
        try
        {
            byte[] b = new byte[1000];
            ois.read(b);

            String str = b.toString();
            System.out.println(str);
            StringTokenizer st = new StringTokenizer(str, "#");
            int x;
            int y;
            x = Integer.valueOf(st.nextToken());
            y = Integer.valueOf(st.nextToken());
            ControllerGrille cg = new ControllerGrille();
            if(cg.coupPossible(x, y)){
                Plateau.getInstance().posePion(x, y, cg.getCasesAFlip());
            }
        }
        catch (IOException ex)
        {
            Logger.getLogger(ReseauLan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
